package com.vm.action.messageBoard;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.action.phoneInfo.PhoneInfoAction;
import com.vm.common.Page;
import com.vm.dao.messageBoard.MessageBoardDao;
import com.vm.dao.messageReply.MessageReplyDao;
import com.vm.entity.MessageBoard;
import com.vm.entity.MessageReply;
import com.vm.log4j.CustomizeLog4j;
import com.vm.util.SQLUtils;

public class MessageBoardAction extends ActionSuper implements Preparable {
	
	private static Logger log = CustomizeLog4j.getLogger(MessageBoardAction.class);
	
	private MessageBoardDao messageBoardDao;
	private MessageReplyDao messageReplyDao;

	private String pageLinkList;
	
	
	private Page<MessageBoard> page;
	
	private String phoneBrand;
	

	private Map<String,String> brandMap;
	
	
	private Integer brandId;
	
	//存放回复内容
	 private String answerContent;
	 
	 private String answerId;
	 
	 //留言的id
	 private String boardId;
	 
	
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
//		this.brandMap = this.brandDao.getSelectPoMap("select id as id,brandName as name from Brand");

	}
	/**
	 *  实现的列表化手机信息
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
//		String sql = SQLUtils.getmessageBoardPageSQL(this.phoneBrand);
		page = null;
		page = this.messageBoardDao.page(PAGE_ROW_SIZE, super.getPageNumber());
		this.pageLinkList =super.getSplitPageLink("/msg/messageBoardAction_list.do?", page);
		
		return SUCCESS;
	}
	/**
	 * 留言回复
	 * @return
	 * @throws Exception
	 */
    public String addReply() throws Exception{
    	String[] msgArg = new String[] { "messageBoardAction_list.do"};
		String msg = null;
		try {
			if(boardId == null || "".equals(boardId)){
				msg = super.getResourceMessage("缺失留言Id无法增加！", msgArg);
				super.setShowMessage(msg);
				return super.SHOW_MESSAGE_RESULT;
			}
			MessageReply  messageReplyTmp = new MessageReply();
			MessageBoard msgBoard = messageBoardDao.findById(Integer.valueOf(boardId));
			messageReplyTmp.setMessageBoard(msgBoard);
			messageReplyTmp.setLhLr(answerContent);
			messageReplyTmp.setLhSj(new Timestamp(new Date().getTime()));
			//后面更换权限
			messageReplyTmp.setUserId(1);
			messageReplyTmp.setUserName("admin");
			messageReplyDao.save(messageReplyTmp);
			messageReplyTmp = null;
			msgBoard = null;
			msg = super.getResourceMessage("insert_messageReply_success", msgArg);
		    page = null;
		} catch (Exception e) {
			log.error("messageReply save error ", e);
			msg = super.getResourceMessage("insert_messageReply_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
    }
    
    public String editReply() throws Exception{
    	
    	String[] msgArg = new String[] { "messageBoardAction_list.do"};
		String msg = null;
		try {
			if(answerId == null || "".equals(answerId)){
				msg = super.getResourceMessage("缺失回复Id无法修改！", msgArg);
				super.setShowMessage(msg);
				return super.SHOW_MESSAGE_RESULT;
			}
			MessageReply  messageReplyTmp = messageReplyDao.findById(Integer.valueOf(answerId));
			messageReplyTmp.setLhLr(answerContent);
			messageReplyTmp.setLhSj(new Timestamp(new Date().getTime()));
			//后面更换权限
			messageReplyTmp.setUserId(1);
			messageReplyTmp.setUserName("admin");
			messageReplyDao.update(messageReplyTmp);
			msg = super.getResourceMessage("update_messageReply_success", msgArg);
			
		} catch (Exception e) {
			log.error("update_messageReply error !", e);
			msg = super.getResourceMessage("update_messageReply_error", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
    }
    
    public String delReply() throws Exception{
    	String[] msgArg = new String[] { "messageBoardAction_list.do"};
		String msg = null;
		try {
			if(answerId == null || "".equals(answerId)){
				msg = super.getResourceMessage("缺失回复Id无法删除！", msgArg);
				super.setShowMessage(msg);
				return super.SHOW_MESSAGE_RESULT;
			}
			messageReplyDao.deleteById(Integer.valueOf(answerId));
			answerId = null;
			msg = super.getResourceMessage("delete_messageReply_success", msgArg);
		} catch (Exception e) {
			log.error("delete messageRepl  error !", e);
			msg = super.getResourceMessage("delete_messageReply_erro", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
    }

   	public String delete() throws Exception {
   		String[] msgArg = new String[] { "messageBoardAction_list.do"};
		String msg = null;
		try {
			
			if(boardId == null || "".equals(boardId)){
				msg = super.getResourceMessage("缺失留言Id无法删除！", msgArg);
				super.setShowMessage(msg);
				return super.SHOW_MESSAGE_RESULT;
			}
			String sql = SQLUtils.delMessageReplySQL();
			//拼装值
			Object[] params = new Object[1];
			params[0] = Integer.valueOf(boardId);
			//删除留言板回复信息
			messageReplyDao.deleteByHql(sql, params);
			//删除留言板信息
			messageBoardDao.deleteById(Integer.valueOf(boardId));
			
			boardId = null;
			page  = null;
			msg = super.getResourceMessage("delete_messageBoard_success", msgArg);
			
		} catch (Exception e) {
			log.error("delete messageBoard  error !", e);
			msg = super.getResourceMessage("delete_messageBoard_erro", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}
   	public MessageBoardDao getMessageBoardDao() {
		return messageBoardDao;
	}
	public void setMessageBoardDao(MessageBoardDao messageBoardDao) {
		this.messageBoardDao = messageBoardDao;
	}
	public MessageReplyDao getMessageReplyDao() {
		return messageReplyDao;
	}
	public void setMessageReplyDao(MessageReplyDao messageReplyDao) {
		this.messageReplyDao = messageReplyDao;
	}
	
	public String getPageLinkList() {
		return pageLinkList;
	}
	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}
	public Page<MessageBoard> getPage() {
		return page;
	}
	public void setPage(Page<MessageBoard> page) {
		this.page = page;
	}
	public String getPhoneBrand() {
		return phoneBrand;
	}
	public void setPhoneBrand(String phoneBrand) {
		this.phoneBrand = phoneBrand;
	}
	public Map<String, String> getBrandMap() {
		return brandMap;
	}
	public void setBrandMap(Map<String, String> brandMap) {
		this.brandMap = brandMap;
	}
	public Integer getBrandId() {
		return brandId;
	}
	public void setBrandId(Integer brandId) {
		this.brandId = brandId;
	}
	public String getAnswerContent() {
		return answerContent;
	}
	public void setAnswerContent(String answerContent) {
		this.answerContent = answerContent;
	}
	public String getAnswerId() {
		return answerId;
	}
	public void setAnswerId(String answerId) {
		this.answerId = answerId;
	}
    
	public String getBoardId() {
		return boardId;
	}
	public void setBoardId(String boardId) {
		this.boardId = boardId;
	}

}
